package br.com.threads.entendendo;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ExemploFrame extends JFrame{
	private JPanel canvas;
	private Cruzamento cruzamento;
	private Carro carro;
	private Carro carro2;
	private Carro carro3;
	private Carro carro4;
	
	//define tamanho da tela e os botoes da aplicacao
	public ExemploFrame(){
		setSize(640,480);
		setTitle("Exemplo Cruzamento");
		setResizable(true);
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		Container contentPane= getContentPane();
		canvas = new JPanel();
		contentPane.add(canvas,"Center");
		JPanel p = new JPanel();
		
		addButton(p,"Iniciar", new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(!carro.isAlive()){
					cruzamento.start();
					carro.start();
					carro2.start();
					carro3.start();
					carro4.start();
				}				
			}
		});
		addButton(p,"Fechar",new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);				
			}
		});
		contentPane.add(p,"South");
	}

	private void addButton(Container c, String title,
			ActionListener a) {
		JButton b = new JButton(title);
		b.addActionListener(a);
		c.add(b);
		
	}
	public void paint(Graphics g){
		super.paint(g);
		//cria o cruzamento
		this.cruzamento = new Cruzamento(canvas);
		//cria os carros
		this.carro = new Carro(canvas, Color.BLUE, Carro.EIXOX, 0, 235, Carro.LESTE_OESTEX, cruzamento);
		this.carro2 = new Carro(canvas, Color.BLACK, Carro.EIXOY, 255, 0, Carro.NORTE_SULY, cruzamento);
		this.carro3 = new Carro(canvas, Color.BLUE, Carro.EIXOX, 640, 165, Carro.OESTE_LESTEX, cruzamento);
		this.carro4 = new Carro(canvas, Color.BLACK, Carro.EIXOY, 345, 400, Carro.SUL_NORTEY, cruzamento);
		
		//bloco para desenhar os elementos na tela
		Graphics2D g2 = (Graphics2D)g;
		
		g2.setColor(Color.BLACK);
		g2.setStroke(new BasicStroke(5));
		g2.draw(new Rectangle2D.Double(0,180,640,130));
		
		g2.setStroke(new BasicStroke(5));
		g2.draw(new Rectangle2D.Double(245,0,150,480));
		
		float dashes [] = {12};
		g2.setColor(Color.YELLOW);
		g2.setStroke(new BasicStroke(4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,
				10,dashes,10));
		g2.draw(new Line2D.Double(0,248,250,248));
		
		g2.setStroke(new BasicStroke(4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,
				10,dashes,10));
		g2.draw(new Line2D.Double(400,248,640,248));
		
		g2.setStroke(new BasicStroke(4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,
				10,dashes,10));
		g2.draw(new Line2D.Double(320,0,320,180));		
		
		g.setColor(Color.BLACK);  
	    g.drawOval(205, 145, 27, 27);  
	    g.drawOval(405, 320, 27, 27);  
	    g.drawOval(405, 145, 27, 27);  
	    g.drawOval(205, 320, 27, 27);  
	}
}
